package com.eduplatform.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eduplatform.common.Result;
import com.eduplatform.dto.SchoolDTO;
import com.eduplatform.entity.School;
import com.eduplatform.service.SchoolService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/schools")
@CrossOrigin
public class SchoolController {

    @Autowired
    private SchoolService schoolService;

    @GetMapping("/list")
    public Result<Page<School>> listSchools(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "size", defaultValue = "10") Integer size) {
        Page<School> schoolPage = schoolService.listSchools(page, size);
        return Result.success(schoolPage);
    }

    @GetMapping("/all")
    public Result<List<School>> getAllSchools() {
        List<School> schools = schoolService.getAllSchools();
        return Result.success(schools);
    }

    @GetMapping("/search")
    public Result<Page<School>> searchSchools(
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "size", defaultValue = "10") Integer size) {
        Page<School> schoolPage = schoolService.searchSchoolsByName(name, page, size);
        return Result.success(schoolPage);
    }

    @GetMapping("/{id}")
    public Result<School> getSchoolById(@PathVariable Long id) {
        School school = schoolService.getById(id);
        if (school != null) {
            return Result.success(school);
        } else {
            return Result.error("学校不存在");
        }
    }

    @PostMapping("/create")
    public Result<Boolean> createSchool(@RequestBody @Valid SchoolDTO schoolDTO) {
        School school = new School();
        BeanUtils.copyProperties(schoolDTO, school);

        // 设置默认值
        school.setCreateTime(new Date());
        school.setUpdateTime(new Date());
        school.setIsDeleted(0);

        boolean result = schoolService.save(school);
        if (result) {
            return Result.success(true);
        } else {
            return Result.error("创建学校失败");
        }
    }

    @PutMapping("/update")
    public Result<Boolean> updateSchool(@RequestBody @Valid SchoolDTO schoolDTO) {
        School school = schoolService.getById(schoolDTO.getId());
        if (school == null) {
            return Result.error("学校不存在");
        }

        BeanUtils.copyProperties(schoolDTO, school);
        school.setUpdateTime(new Date());

        boolean result = schoolService.updateById(school);
        if (result) {
            return Result.success(true);
        } else {
            return Result.error("更新学校失败");
        }
    }

    @PutMapping("/status/{id}/{status}")
    public Result<Boolean> updateSchoolStatus(@PathVariable Long id, @PathVariable Integer status) {
        boolean result = schoolService.updateStatus(id, status);
        if (result) {
            return Result.success(true);
        } else {
            return Result.error("更新学校状态失败");
        }
    }

    @DeleteMapping("/{id}")
    public Result<Boolean> deleteSchool(@PathVariable Long id) {
        boolean result = schoolService.removeById(id);
        if (result) {
            return Result.success(true);
        } else {
            return Result.error("删除学校失败");
        }
    }
}